//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "HullWhite.h"
using namespace Cephei::QL::Models::Shortrate::Onefactormodels;
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Lattice.h>
#include <gen/QL/TimeGrid.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/CalibrationHelper.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/Constraint.h>
#include <gen/QL/Models/Shortrate/Onefactormodels/Vasicek.h>
#include <gen/QL/Models/TermStructureConsistentModel.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Models;
using namespace Cephei::QL::Math::Optimization;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::CHullWhite (Cephei::QL::Termstructures::IYieldTermStructure^ termStructure, Microsoft::FSharp::Core::FSharpOption<Double>^ a, Microsoft::FSharp::Core::FSharpOption<Double>^ sigma) : CVasicek(CHullWhite::typeid)
{
    CYieldTermStructure^ _CtermStructure;
    try
    {
#ifdef HANDLE
        _phHullWhite = NULL;
#endif
        _CtermStructure = safe_cast<CYieldTermStructure^> (termStructure);
        _CtermStructure->Lock();
        Handle<QuantLib::YieldTermStructure>& _termStructure = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CtermStructure->GetHandle ()); 
        QuantLib::Real _a = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (a) ? (QuantLib::Real)ValueHelper::Convert (a->Value) : 0.1); //4
        QuantLib::Real _sigma = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (sigma) ? (QuantLib::Real)ValueHelper::Convert (sigma->Value) : 0.01); //4
        _ppHullWhite = new boost::shared_ptr<QuantLib::HullWhite> (new QuantLib::HullWhite ( _termStructure,  _a,  _sigma ));
        SetVasicek (boost::dynamic_pointer_cast<QuantLib::Vasicek> (*_ppHullWhite));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CtermStructure != nullptr) _CtermStructure->Unlock();
    }
}
Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::CHullWhite (boost::shared_ptr<QuantLib::HullWhite>& childNative, Object^ owner) : CVasicek(CHullWhite::typeid)
{
#ifdef HANDLE
	_phHullWhite = NULL;
#endif
	_ppHullWhite = &childNative;
    _ppVasicek = new boost::shared_ptr<QuantLib::Vasicek> (boost::dynamic_pointer_cast<QuantLib::Vasicek> (*_ppHullWhite));
}
Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::CHullWhite (QuantLib::HullWhite& childNative, Object^ owner) : CVasicek(CHullWhite::typeid)
{
#ifdef HANDLE
	_phHullWhite = NULL;
#endif
	_ppHullWhite = new boost::shared_ptr<QuantLib::HullWhite> (&childNative);
    _ppVasicek = new boost::shared_ptr<QuantLib::Vasicek> (boost::dynamic_pointer_cast<QuantLib::Vasicek> (*_ppHullWhite));
    _HullWhiteOwner = owner;
    _VasicekOwner = owner;
}

Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::CHullWhite (CHullWhite^ copy) : CVasicek(CHullWhite::typeid)
{
#ifdef HANDLE
	_phHullWhite = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppHullWhite = new boost::shared_ptr<QuantLib::HullWhite> (copy->GetShared());
        _ppVasicek = new boost::shared_ptr<QuantLib::Vasicek> (boost::dynamic_pointer_cast<QuantLib::Vasicek> (*_ppHullWhite));
    }
}
Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::CHullWhite (System::Type^ t) : CVasicek(CHullWhite::typeid)
{
#ifdef HANDLE
	_phHullWhite = NULL;
#endif
	if (!t->IsSubclassOf(CHullWhite::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::CHullWhite (QuantLib::Handle<QuantLib::HullWhite>& childNative, Object^ owner)  : CVasicek(CHullWhite::typeid)
{
	_phHullWhite = &childNative;
	_ppHullWhite = &static_cast<boost::shared_ptr<QuantLib::HullWhite>>(childNative.currentLink());
    _ppVasicek = new boost::shared_ptr<QuantLib::Vasicek> (boost::dynamic_pointer_cast<QuantLib::Vasicek> (*_ppHullWhite));
    _HullWhiteOwner = owner;
}
Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::CHullWhite (QuantLib::Handle<QuantLib::HullWhite> childNative)  : CVasicek(CHullWhite::typeid)
{
	_phHullWhite = &childNative;
	_ppHullWhite = &static_cast<boost::shared_ptr<QuantLib::HullWhite>>(childNative.currentLink());
    _ppVasicek = new boost::shared_ptr<QuantLib::Vasicek> (boost::dynamic_pointer_cast<QuantLib::Vasicek> (*_ppHullWhite));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::CHullWhite (QuantLib::HullWhite childNative)  : CVasicek(CHullWhite::typeid)
{
#ifdef HANDLE
	_phHullWhite = NULL;
#endif
	_ppHullWhite = new boost::shared_ptr<QuantLib::HullWhite> (new QuantLib::HullWhite (childNative));
    _ppVasicek = new boost::shared_ptr<QuantLib::Vasicek> (boost::dynamic_pointer_cast<QuantLib::Vasicek> (*_ppHullWhite));
}
#endif

Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::~CHullWhite ()
{
    if (_ppHullWhite != NULL)
    {
	    delete _ppHullWhite;
        _ppHullWhite = NULL;
    }
}
Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::!CHullWhite ()
{
    if (_ppHullWhite != NULL)
    {
	    delete _ppHullWhite;
    }
}
QuantLib::HullWhite& Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::GetReference ()
{
    if (_ppHullWhite == NULL) throw gcnew NativeNullException ();
	return **_ppHullWhite;
}
boost::shared_ptr<QuantLib::HullWhite>& Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::GetShared ()
{
    if (_ppHullWhite == NULL) throw gcnew NativeNullException ();
	return *_ppHullWhite;
}
QuantLib::HullWhite* Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::GetPointer ()
{
    if (_ppHullWhite == NULL) throw gcnew NativeNullException ();
	return &**_ppHullWhite;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::HullWhite>& Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::GetHandle ()
{
	if (_phHullWhite == NULL)
	{
		_phHullWhite = new Handle<QuantLib::HullWhite> (*_ppHullWhite);
	}
	return *_phHullWhite;
}
#endif
bool Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::HasNative () 
{
	return (_ppHullWhite != NULL);
}

Double Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::ConvexityBias (Double futurePrice, Double t, Double T2, Double sigma, Double a)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _futurePrice = (QuantLib::Real)ValueHelper::Convert (futurePrice);
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Time _T2 = (QuantLib::Time)ValueHelper::Convert (T2);
        QuantLib::Real _sigma = (QuantLib::Real)ValueHelper::Convert (sigma);
        QuantLib::Real _a = (QuantLib::Real)ValueHelper::Convert (a);
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppHullWhite)->convexityBias ( _futurePrice,  _t,  _T2,  _sigma,  _a );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::DiscountBondOption (QL::Option::TypeEnum type, Double strike, Double maturity, Double bondMaturity)
{
    try
    {
        QuantLib::Option::Type _type = (QuantLib::Option::Type)type ;
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike);
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity);
        QuantLib::Time _bondMaturity = (QuantLib::Time)ValueHelper::Convert (bondMaturity);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhite)->discountBondOption ( _type,  _strike,  _maturity,  _bondMaturity );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::ILattice^ Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite::Tree (Cephei::QL::ITimeGrid^ grid)
{
    CTimeGrid^ _Cgrid;
    try
    {
        _Cgrid = safe_cast<CTimeGrid^> (grid);
        _Cgrid->Lock();
        QuantLib::TimeGrid& _grid = static_cast<QuantLib::TimeGrid&> (_Cgrid->GetReference ()); 
    	boost::shared_ptr<QuantLib::Lattice> _rv = (boost::shared_ptr<QuantLib::Lattice>)(*_ppHullWhite)->tree ( _grid );   
        Cephei::QL::CLattice^ _nrv = gcnew Cephei::QL::CLattice (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cgrid != nullptr) _Cgrid->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Shortrate::Onefactormodels::IHullWhite^ Cephei::QL::Models::Shortrate::Onefactormodels::CHullWhite_Factory::Create (Cephei::QL::Termstructures::IYieldTermStructure^ termStructure, Microsoft::FSharp::Core::FSharpOption<Double>^ a, Microsoft::FSharp::Core::FSharpOption<Double>^ sigma)
{
    return gcnew CHullWhite ( termStructure,  a,  sigma);
}
